//
//  ToolOperation.m
//  microtv
//
//  Created by zhang jun on 12-3-28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ToolOperation.h"



static ToolOperation* tool = nil;

@implementation ToolOperation


+(ToolOperation*)shareTool{
    
    @synchronized(self){
        
        if ( tool == nil) {
        
            tool = [[ToolOperation alloc] init];
        }
        
    }
    
    return tool;
}

//把字符串时间 转化成时间戳,返回时间戳
-(NSInteger)changeSamTimeToUNIXTime:(NSString*)samTime{
    
     NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [df setTimeZone:[NSTimeZone localTimeZone]];
     
     NSDate* samDate = [df dateFromString:samTime];
     NSInteger timeUnix = [samDate timeIntervalSince1970];
    
    return timeUnix;
}



//把unix时间戳 转化成字符串，返回字符串
-(NSString*)changeUNIXTimeToSamTime:(NSInteger)unixtime{
    
    NSDate* unixdate = [NSDate dateWithTimeIntervalSince1970:unixtime];
    
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [df setTimeZone:[NSTimeZone localTimeZone]];
    NSString* samTime = [df stringFromDate:unixdate];
    return samTime;
}


//把unix时间戳 转化成字符串，返回字符串 简单适用版
-(NSString*)changeUNIXTimeToSamTimeSample:(NSInteger)unixtime{
    
    NSDate* unixdate = [NSDate dateWithTimeIntervalSince1970:unixtime];
    
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    [df setTimeZone:[NSTimeZone localTimeZone]];
    
    NSString* samTime = [df stringFromDate:unixdate];    
    return samTime;
}


//获取当前所在星期的星期一，所在的时间点
//返回周一的时间戳
-(NSInteger)MondayOfweekUNIX{
    
    NSDate *datetoday = [NSDate date];
    NSInteger unixtime = [datetoday timeIntervalSince1970];
    
    //获取距离星期一的毫秒数
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents *cps = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:datetoday]; 
   
    NSInteger diffWeek = [cps weekday];
   // NSLog(@"距离星期一天数 %d",diffWeek-1);
    NSInteger sepSecs =  (diffWeek-2)*24*3600;
    
    //所在星期一那个时间点毫秒数
    NSInteger mondaySec = unixtime -sepSecs;
    
    NSDate* monDat = [NSDate dateWithTimeIntervalSince1970:mondaySec];
    
    NSDateComponents *newcps = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:monDat]; 
    
    NSInteger mday = [newcps day];
    NSInteger mmonth = [newcps month];
    NSInteger myear = [newcps year];

    //获取周一00分00秒的时间戳
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [df setTimeZone:[NSTimeZone localTimeZone]];
    
    NSDate* date = [df dateFromString:[NSString stringWithFormat:@"%d-%d-%d 00:00:00",myear,mmonth,mday]];
   //把这个时间换成时间戳
    NSInteger monUN = [date timeIntervalSince1970];
   // NSLog(@"monUN %d",monUN);
    return monUN;
}




//获取当前时间的字符串
-(NSString*)stringOfCurrentDateTool{
    NSDate* nowdate = [NSDate date];
    NSDateFormatter* daf = [[NSDateFormatter alloc] init];
    [daf setDateFormat:@"yyyy-MM-dd"];
    [daf setTimeZone:[NSTimeZone localTimeZone]];
    NSString* str = [daf stringFromDate:nowdate];
    return str;
}


//获取当前时间的字符串
-(NSString*)stringOfCurrentDateTool2{
    NSDate* nowdate = [NSDate date];
    NSDateFormatter* daf = [[NSDateFormatter alloc] init];
    [daf setDateFormat:@"MM-dd HH:mm"];
    [daf setTimeZone:[NSTimeZone localTimeZone]];
    NSString* str = [daf stringFromDate:nowdate];
    return str;
}

//获取当前时间的时间戳
-(NSInteger)CurrentUNIXTool{
    
    NSDate* nowdate = [NSDate date];    
    return [nowdate timeIntervalSince1970];
}

//获取字符串时间的下面num个时间字符串，传入字符串，返回后面num天的日期字符串
-(NSString*)getNextTimeString:(NSString*)beforetime AndNextNum:(NSInteger)num{

    return nil;
}
     

//获取从当前时间开始的下一个整点时间戳
-(NSInteger)getUNixTimeOFInteger{
    
    NSDate *datetoday = [NSDate date];
    NSInteger unixtime = [datetoday timeIntervalSince1970];//当前时间戳
    
    NSString* strN = [self stringOfCurrentDateTool];
    NSString* minu = [strN substringWithRange:NSMakeRange(14, 2)];
    //NSLog(@"%@",minu);
    
    NSInteger resInt = unixtime + (60 - [minu intValue])* 60;

    return resInt;    
    
}


//获取当前时间的下一个整点时间的时间字符串

-(NSString*)getNextTimeSepterNowOneHour{
    
    NSDate *datetoday = [NSDate date];
    NSInteger unixtime = [datetoday timeIntervalSince1970];//当前时间戳
    
    NSString* strN = [self stringOfCurrentDateTool];
    NSString* minu = [strN substringWithRange:NSMakeRange(14, 2)];
    NSString* sec = [strN substringWithRange:NSMakeRange(17, 2)];
    
    
    NSInteger nextUn = unixtime - [minu intValue]*60 - [sec intValue] + 3600;
    
    NSString* resstr = [self changeUNIXTimeToSamTime:nextUn];
    return resstr;
}

//获取当前时间的时间戳
-(NSString*)getNowUnix{
    
    NSDate* nowDa = [NSDate date];
    NSTimeInterval timeInt = [nowDa timeIntervalSince1970];
    NSString* resStr = [NSString stringWithFormat:@"%0.lf",timeInt];
    return resStr;
}


//获取昨天的时间字符串
-(NSString*)getTimeOfYesterday{

    NSInteger nowInt = [self CurrentUNIXTool];
    NSInteger yesTerInt = nowInt - 24*60*60;
    
   NSString* timeStr = [self changeUNIXTimeToSamTimeSample:yesTerInt];
    
    return timeStr;
}


//获取时间是否是今天，昨天，还是具体时间
-(NSString*)getTimeIsToday:(NSString*)timeStr{
    
    if ( [timeStr isEqualToString:[self stringOfCurrentDateTool]] ) {
     
        return @"今天";
        
    }else if( [timeStr isEqualToString:[self getTimeOfYesterday]] ){
        
        return @"昨天";
        
    }else{
        
        return timeStr;
    }
}

//获取当前系统版本
+(float)getSystemVersion{
    
    return [[[UIDevice currentDevice] systemVersion] floatValue];
}
@end